
#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include "movedirection.h"
#include <QFileDialog>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //text
    length = 0;
    col = 0;
    TimeLevel = 0;
    i=0;
    score = 0;
    map = nullptr;


//****************************************************************************************************************************
   // ui->setupUi(this);

    mPoint = new QGraphicsTextItem;
    mPoint->setPlainText(QString::number(score));
    mPoint->setDefaultTextColor(QColor(255,255,255));//颜色
    mPoint->setFont(QFont("Courier New",23,100));//字体
    mPoint->setPos(800,850);


//****************************************************************************************************************************
    TEXT = new text(this);
    QString fileName = ":/music/text/jhcc1.txt";
    InitByFile(fileName);

    this->setFixedSize(1620,960);

    //视图场景
    mGameView.setSceneRect(QRect(0,0,1620,960));
    mScene.setSceneRect(QRect(0,0,1620,960));
    STARTScene.setSceneRect(QRect(0,0,1620,960));

    startTimer = new QTimer(this);
    startTimer->start(1000);
    connect(startTimer,&QTimer::timeout,[this](){
        startbackground.setPixmap(QPixmap(":/background/C:/Users/Administrator/Pictures/otto-song/start(1).png"));
        QTimer::singleShot(500,[this](){
            startbackground.setPixmap(QPixmap(":/background/C:/Users/Administrator/Pictures/otto-song/start(2).png"));
        });
    });

    //STARTScene.addPixmap(QPixmap(":/background/C:/Users/Administrator/Pictures/otto-song/start(1).png"));
    background.setPixmap(QPixmap(":/background/C:/Users/Administrator/Videos/bilibili/958376458/jhcc.jpg"));


    //otto
    otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_wait.png"));
    otto.setPos(0,300);

    //箭头
    up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up.png"));
    left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left.png"));
    right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right.png"));
    down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down.png"));
    left.setPos(880,30);
    up.setPos(1040,30);
    down.setPos(1200,30);
    right.setPos(1360,30);

    //添加到场景里
    STARTScene.addItem(&startbackground);

    mScene.addItem(&background);
    mScene.addItem(&up);
    mScene.addItem(&left);
    mScene.addItem(&right);
    mScene.addItem(&down);
    mScene.addItem(&otto);
    mScene.addItem(mPoint);
    //mGameView.setScene(&mScene);
    mGameView.setScene(&STARTScene);
    mGameView.setParent(this);

    //设置音乐
    this->Music.setMedia(QUrl("qrc:/C:/Users/Administrator/Videos/start/start.mp3"));
    //this->Music.setMedia(QUrl("qrc:/C:/Users/Administrator/Videos/otto jhcc/otto.mp3"));
    this->Music.setVolume(30);
    this->Music.play();

    //开启按键检测定时器
    DirectPointTimer = new QTimer(this);
    DirectPointTimer->start(1);
    connect(DirectPointTimer,&QTimer::timeout,this,&Widget::DirectChange);
    connect(DirectPointTimer,&QTimer::timeout,this,&Widget::showpoint);

    //开启箭头移动
    DirectMoveTimer = new QTimer(this);
    DirectMoveTimer->start(10);
    connect(DirectMoveTimer,&QTimer::timeout,[this](){
        for(auto movedirection : UPsList)
        {
            movedirection->DMove();
            for(int i = 0;i < UPsList.size();i++)
            {
                if(UPsList[i]->y()<-160)
                {
                    mScene.removeItem(UPsList[i]);
                    delete UPsList[i];
                    UPsList.removeOne(UPsList[i]);
                }
            }
        }
        for(auto movedirection : DOWNsList)
        {
            movedirection->DMove();
            for(int i = 0;i < DOWNsList.size();i++)
            {
                if(DOWNsList[i]->y()<-160)
                {
                    mScene.removeItem(DOWNsList[i]);
                    delete DOWNsList[i];
                    DOWNsList.removeOne(DOWNsList[i]);
                }
            }
        }
        for(auto movedirection : RIGHTsList)
        {
            movedirection->DMove();
            for(int i = 0;i < RIGHTsList.size();i++)
            {
                if(RIGHTsList[i]->y()<-160)
                {
                    mScene.removeItem(RIGHTsList[i]);
                    delete RIGHTsList[i];
                    RIGHTsList.removeOne(RIGHTsList[i]);
                }
            }
        }
        for(auto movedirection : LEFTsList)
        {
            movedirection->DMove();
            for(int i = 0;i < LEFTsList.size();i++)
            {
                if(LEFTsList[i]->y()<-160)
                {
                    mScene.removeItem(LEFTsList[i]);
                    delete LEFTsList[i];
                    LEFTsList.removeOne(LEFTsList[i]);
                }
            }
        }
    });
}

Widget::~Widget()
{
    delete ui;
}



//text***********************************************************************************************************************
void Widget::InitByFile(QString fileName)
{
    QFile file(fileName);//创建文件对象
    file.open(QIODevice::ReadOnly);

    //读取内容
    QByteArray arrALL = file.readAll();
    arrALL.replace("\r\n","\n");
    QList<QByteArray> NoteList = arrALL.split('\n');
    length = NoteList.size();//确定谱面长度
    map = new int*[length];

    for(int i=0;i<length;i++)
    {
        QList<QByteArray> collist = NoteList[i].split(',');
        col = collist.size();
        map[i] = new int[col];

        for (int j=0;j<col;j++)
        {
            map[i][j] = collist[j].toInt();
            qDebug() << map[i][j];
        }
    }

    paint0();
}

//paint**************************************************************************************************************

void Widget::paint1()
{
    for(;i<length;i++)
    {
        time = map[i][0] - 20000;
        qDebug() << time ;
        if(time < 20000 && time > 0)
        {
            if(map[i][1] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionLEFT()));
            }
            if(map[i][2] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionUP()));
            }
            if(map[i][3] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionDOWN()));
            }
            if(map[i][4] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionRIGHT()));
            }
        }
        else if(time > 20000)//time > 40s
        {
            TimeTimer1->stop();
            delete TimeTimer1;
            TimeTimer2 = new QTimer(this);
            TimeTimer2->setInterval(20000);
            TimeTimer2->start();
            connect(TimeTimer2,SIGNAL(timeout()),this,SLOT(paint2()));
            break;
        }
    }
}

void Widget::paint0()
{
    for(int i=0;i<length;i++)
    {
        time = map[i][0];
        qDebug() << time ;
        if(time < 20000 )
        {
            if(map[i][1] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionLEFT()));
            }
            if(map[i][2] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionUP()));
            }
            if(map[i][3] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionDOWN()));
            }
            if(map[i][4] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionRIGHT()));
            }
        }
        else//time > 20s
        {
            TimeTimer1 = new QTimer(this);
            TimeTimer1->setInterval(20000);
            TimeTimer1->start();
            connect(TimeTimer1,SIGNAL(timeout()),this,SLOT(paint1()));
            break;
        }
    }
}

void Widget::paint2()
{
    for(;i<length;i++)
    {
        time = map[i][0] - 40000;
        qDebug() << time ;
        if(time < 20000 && time > 0)
        {
            if(map[i][1] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionLEFT()));
            }
            if(map[i][2] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionUP()));
            }
            if(map[i][3] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionDOWN()));
            }
            if(map[i][4] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionRIGHT()));
            }
        }
        else if(time > 20000)//time > 60s
        {

            TimeTimer2->stop();
            delete TimeTimer2;
            TimeTimer3 = new QTimer(this);
            TimeTimer3->setInterval(20000);
            TimeTimer3->start();
            connect(TimeTimer3,SIGNAL(timeout()),this,SLOT(paint3()));
            break;
        }
    }
}

void Widget::paint3()
{
    for(;i<length;i++)
    {
        time = map[i][0] - 60000;
        qDebug() << time ;
        if(time < 20000 && time > 0)
        {
            if(map[i][1] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionLEFT()));
            }
            if(map[i][2] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionUP()));
            }
            if(map[i][3] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionDOWN()));
            }
            if(map[i][4] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionRIGHT()));
            }
        }
        else if(time > 20000)//time > 80s
        {
            TimeTimer3->stop();
            delete TimeTimer3;
            TimeTimer4 = new QTimer(this);
            TimeTimer4->setInterval(20000);
            TimeTimer4->start();
            connect(TimeTimer4,SIGNAL(timeout()),this,SLOT(paint4()));
            break;
        }
    }
}

void Widget::paint4()
{
    for(;i<length;i++)
    {
        time = map[i][0] - 80000;
        qDebug() << time ;
        if(time < 20000 && time > 0)
        {
            if(map[i][1] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionLEFT()));
            }
            if(map[i][2] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionUP()));
            }
            if(map[i][3] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionDOWN()));
            }
            if(map[i][4] == 1)
            {
                QTimer::singleShot(time,this,SLOT(MakeDirectionRIGHT()));
            }
        }
        else
        {
            TimeTimer4->stop();
            delete TimeTimer4;
        }
    }
}

void Widget::clear()
{
    if(map != nullptr)
    {
        for(int i=0;i<length;i++)
        {
            delete[] map[i];
        }
        delete[] map;
    }
}

void Widget::showpoint()
{
    mPoint->setPlainText(QString::number(score));
}

//创建移动箭头*********************************************************************************************************************
void Widget::MakeDirectionUP()
{
    //创建箭头UP
    QPixmap upImg(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up_point.png");
    QPoint pos(1040,1000);
    MoveDirection* move_up =new MoveDirection(pos,upImg,MoveDirection::DI_up);
    //添加到场景
    mScene.addItem(move_up);
    //添加到管理器
    UPsList.append(move_up);
}
void Widget::MakeDirectionLEFT()
{
    //创建箭头LEFT
    QPixmap leftImg(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left_point.png");
    QPoint pos(880,1000);
    MoveDirection* move_left =new MoveDirection(pos,leftImg,MoveDirection::DI_left);
    //添加到场景
    mScene.addItem(move_left);
    //添加到管理器
    LEFTsList.append(move_left);
}
void Widget::MakeDirectionDOWN()
{
    //创建箭头DOWN
    QPixmap downImg(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down_point.png");
    QPoint pos(1200,1000);
    MoveDirection* move_down =new MoveDirection(pos,downImg,MoveDirection::DI_down);
    //添加到场景
    mScene.addItem(move_down);
    //添加到管理器
    DOWNsList.append(move_down);
}
void Widget::MakeDirectionRIGHT()
{
    //创建箭头RIGHT
    QPixmap rightImg(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right_point.png");
    QPoint pos(1360,1000);
    MoveDirection* move_right =new MoveDirection(pos,rightImg,MoveDirection::DI_right);
    //添加到场景
    mScene.addItem(move_right);
    //添加到管理器
    RIGHTsList.append(move_right);
}





//按按键后箭头变色******************************************************************************************************************
void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加按键到按键 组合
    switch(event->key()){
    case Qt::Key_S:
    {
        for(int i =0;i<UPsList.size();i++)
        {
            if(UPsList[i]->y() <= up.y() + 80 && UPsList[i]->y() >= up.y() - 80)
            {

                score += 10;
                mScene.removeItem(UPsList[i]);
                delete UPsList[i];
                UPsList.removeOne(UPsList[i]);
            }
        }
        KeyList.append(event->key());
        break;
    }
    case Qt::Key_A:
    {
        for(int i =0;i<LEFTsList.size();i++)
        {
            if(LEFTsList[i]->y() <= left.y() + 80 && LEFTsList[i]->y() >= left.y() - 80)
            {
                score += 10;
                mScene.removeItem(LEFTsList[i]);
                delete LEFTsList[i];
                LEFTsList.removeOne(LEFTsList[i]);
            }
        }
        KeyList.append(event->key());
        break;
    }
    case Qt::Key_J:
    {
        for(int i =0;i<DOWNsList.size();i++)
        {
            if(DOWNsList[i]->y() <= down.y() + 80 && DOWNsList[i]->y() >= down.y() - 80)
            {
                score += 10;
                mScene.removeItem(DOWNsList[i]);
                delete DOWNsList[i];
                DOWNsList.removeOne(DOWNsList[i]);
            }
        }
        KeyList.append(event->key());
        break;
    }
    case Qt::Key_K:
    {
        for(int i =0;i<RIGHTsList.size();i++)
        {
            if(RIGHTsList[i]->y() <= right.y() + 80 && RIGHTsList[i]->y() >= right.y() - 80)
            {
                score += 10;
                mScene.removeItem(RIGHTsList[i]);
                delete RIGHTsList[i];
                RIGHTsList.removeOne(RIGHTsList[i]);
            }
        }
        KeyList.append(event->key());
        break;
    }
    }
}

void Widget::DirectChange()
{
    for(int keyCode : KeyList)
    {
        switch(keyCode)
        {
        case Qt::Key_S:
        {
            up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up_point.png"));
            otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_up.png"));
            break;
        }
        case Qt::Key_A:
        {
            otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_left.png"));
            left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left_point.png"));
            break;
        }
        case Qt::Key_J:
        {
            down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down_point.png"));
            otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_down.png"));
            break;
        }
        case Qt::Key_K:
        {
            right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right_point.png"));
            otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_right.png"));
            break;
        }
        }
    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(KeyList.contains(event->key()))
    {
        KeyList.removeOne(event->key());
    }
    switch(event->key()){
    case Qt::Key_S:
    {
        up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up.png"));
        QTimer::singleShot(500,[this](){otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_wait.png"));});
        break;
    }
    case Qt::Key_A:
    {
        left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left.png"));
        QTimer::singleShot(500,[this](){otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_wait.png"));});
        break;
    }
    case Qt::Key_J:
    {
        down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down.png"));
        QTimer::singleShot(500,[this](){otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_wait.png"));});
        break;
    }
    case Qt::Key_K:
    {
        right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right.png"));
        QTimer::singleShot(500,[this](){otto.setPixmap(QPixmap(":/otto/C:/Users/Administrator/Pictures/otto-song/otto/ottoS_wait.png"));});
        break;
    }
    }
}
